package 链表;

public class Solution203 {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2, node1);
        ListNode node3 = new ListNode(6, node2);
        ListNode node4 = new ListNode(3, node3);
        ListNode node5 = new ListNode(2, node4);
        ListNode node = removeElements(node5, 2);
        System.out.println(node);

    }

    public static ListNode removeElements(ListNode head, int val) {

        ListNode result = new ListNode(-1, head);
        ListNode pre = result;
        ListNode cur = head;

        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;

        }
        return result.next;

    }
}
